﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 地址组管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class IPGroupManagerController : AdminBaseController
    {
        //
        // GET: /Admin/IPGroupManager/
        /// <summary>
        /// 地址组管理主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_IPGroupManager", Order = 1)]
        [LogPageActionFilter("IP地址组管理", Order = 2)]
        public ActionResult Index()
        {
            int pageIndex = 1;
            List<Model.IPGroup> groupList = GetIPGroupList(pageIndex);

            return View(groupList);
        }


        /// <summary>
        /// 用于翻页刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult IPGroupManagerListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string adNo = Request.Params["adNo"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }
                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            List<Model.IPGroup> groupList = GetIPGroupList(pageIndex, pageCount, pageRowSize);


            return PartialView("IPGroupManagerListPartial", groupList);
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageCount">每页显示大小</param>
        /// <param name="pageRowSize">每页显示页数</param>
        /// <returns>集合</returns>
        private List<Model.IPGroup> GetIPGroupList(int pageIndex, int pageCount = 0, int pageRowSize = 0)
        {
            BLL.IPGroup groupBll = new BLL.IPGroup();
            BLL.Common commonBll = new BLL.Common();
            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("t_IPGroup", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "FIPGroupID", 0, out total);

            List<Model.IPGroup> groupAllList = new List<Model.IPGroup>();
            List<Model.IPGroup> groupList = groupBll.DataTableToListIPGroup(ds.Tables[0]);

            foreach (Model.IPGroup model in groupList)
            {
                model.StrFStartIP = GenerateUtil.LongToIP(model.FStartIP);
                model.StrFEndIP = GenerateUtil.LongToIP(model.FEndIP);             
            }
           
            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;

            for (int i = 0; i < total; i++)
            {
                groupAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (groupList.Count > i)
                {
                    groupAllList[((pageIndex - 1) * pageRowSize) + i] = groupList[i];
                }
            }

            return groupAllList;
        }

        /// <summary>
        /// 增加页面
        /// </summary>      
        /// <param name="groupID">主键ID</param>
        /// <returns>视图</returns>
        public ActionResult IPGroupManagerEdit(string groupID)
        {
            Model.IPGroup groupModel = new Model.IPGroup();
            BLL.IPGroup groupBll = new BLL.IPGroup();
           
            if (groupID == null || groupID == "")
            {
                ViewData["type"] = "ADD";              
                ViewData["groupID"] = groupID;
                ViewData["startIP"] = "";
                ViewData["endIP"] = "";
            }
            else
            {
                ViewData["type"] = "EDIT";
                ViewData["groupID"] = groupID;
                groupModel = groupBll.GetModel(Int32.Parse(groupID));
                string startIP = GenerateUtil.LongToIP(groupModel.FStartIP);
                ViewData["startIP"] = startIP;
                string endIP = GenerateUtil.LongToIP(groupModel.FEndIP);
                ViewData["endIP"] = endIP;
                
            }


            return View(groupModel);
        }


        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="groupModel">groupModel对象</param>
        /// <returns>视图</returns>
        public ActionResult Save(Model.IPGroup groupModel) {
            string type = Fetch.Post("hdtype");
            ViewData["startIP"] = Fetch.Post("hdstartIP");
            ViewData["endIP"] = Fetch.Post("hdendIP");
            BLL.IPGroup groupBll = new BLL.IPGroup();
            try{
                switch (type)
                {
                    case "ADD":
                        {
                            groupModel.FIPGroupID = groupModel.FIPGroupID;
                            groupModel.FEntityNo = GlobalConstant.ENTITYNO;
                            groupModel.FIPGroupName = Fetch.Post("tbFIPgroupName");
                            string startIP = Fetch.Post("tbFStartIP");
                            string endIP = Fetch.Post("tbFEndIP");
                            groupBll.Add(groupModel, startIP, endIP);
                            break;
                        }
                    case "EDIT":
                        {
                            groupModel = groupBll.GetModel(Int32.Parse(Fetch.Post("hdID")));
                            groupModel.FIPGroupID = groupModel.FIPGroupID;
                            groupModel.FEntityNo = GlobalConstant.ENTITYNO;
                            groupModel.FIPGroupName = Fetch.Post("tbFIPgroupName");
                            string startIP = Fetch.Post("tbFStartIP");
                            string endIP = Fetch.Post("tbFEndIP");
                            groupBll.Update(groupModel, startIP, endIP);
                            break;
                        }
                }
                ViewData["jsAlert"] = "OK";
            }
            catch (Exception)
            {
                ViewData["jsAlert"] = "error";
                
            }


            return View("IPGroupManagerEdit", groupModel);    
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="groupID">编号</param>
        /// <returns>字符串str</returns>
        public string IPGroupManagerDelete(string groupID)
        {
            string str = "";
            groupID = Fetch.estimateString(groupID, CheckGetEnum.Safety);
            BLL.IPGroup groupBll = new BLL.IPGroup();
            try {

                groupBll.Delete(int.Parse(groupID));
                str = "ok";
            }
            catch (Exception) {
                str = "error";
            }

            return str;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="groupIDs">编号</param>
        /// <returns>字符串</returns>
        public string IPGroupManagerMDelete(string groupIDs)
        {
            string str = "";
            string[] groupIDArray = groupIDs.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);
            BLL.IPGroup groupBll = new BLL.IPGroup();
            try
            {
                groupBll.DelMany(groupIDArray, GlobalConstant.ENTITYNO);
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }
            return str;
        }

    }
}
